<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('article_class', LIB_PATH);
$main = new Article($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;
require_once(INFO_PATH.'cf_article_cate.php');

switch ($cf_action) {
    case "index":
	$article_cate = add_array_item(array('' => '全部'), $article_cate);
	$smarty->mypage($cf_controller, $cf_action, 0);	
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode($_GET['skey2']);
	$skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode($_GET['skey3']);
	$skey4 = isset($_POST['skey4']) ? trim($_POST['skey4']) : urldecode($_GET['skey4']);	
	$where = '1';
	if ($skey) {
	    $where .= " and article_title like '%" . $skey . "%'";
	}	
	if ($skey2 && $skey3) {
            $where .= " AND DATE_FORMAT(article_create_time,'%Y-%m-%d')  BETWEEN '".$skey2."' AND '".$skey3."'";
    }
	
	
	if($skey4){
		$where .= " and article_cate_id = '" . $skey4 . "'";
	}	
	


	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "article_id DESC ", intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt . '&skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3 . '&skey4=' . $skey4);
	
	//$match = get_image_url($data_ary[3]['article_content']);
	//$str = '<img src="http://s9.sinaimg.cn/middle/7033e3d7t768a0d364ac8&.jpg" width="491" height="690"/>';
	

	//preg_match_all($pattern,$str,$match);
	//echo "<pre>"; 
	//print_r($match);
	
	//echo $MyPage->get_sql();
	//dump($data_ary);
	break;
    case "add":
	if ($cf_ispost) {
		$data = $db->getFormData($main->get_table_name(),$_POST);
		$data['article_content'] = htmlspecialchars(stripcslashes($_POST['article_content']));
		$data['article_create_authorid'] = $cf_admin_info['admin_id'];				
		$data['article_create_time'] = date("Y-m-d H:i:s");		
	    $result = $main->insert_data($data);		
	    if ($result) {
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);
			$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
			my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
			my_jump('添加失败');
	    }
	} else {

	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		
	}

	break;
    case "edit":
	if ($cf_ispost) {
	    $data = $db->getFormData($main->get_table_name(),$_POST);
			$data['article_edit_authorid'] = $cf_admin_info['admin_id'];		
		$data['article_edit_time'] = date("Y-m-d H:i:s");
		$data['article_content'] = stripcslashes($data['article_content']);			
		$data['article_content'] = str_replace('\"',"",$data['article_content'] );
		//dump($data);
		//$data['article_content'] = htmlspecialchars($data['article_content'],ENT_QUOTES);
		$data['article_content'] = htmlspecialchars(stripcslashes($_POST['article_content']));		
	    $result = $main->update_data($data, "article_id='$id'");

	    if ($result) {
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);
		    my_success('修改成功', '', $return_page, 3);
	    } else {
		    my_error('修改失败');
	    }
	} else {		
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
			$row = $main->get_data_row($id);
			if (!$row)
				my_jump('沒有此信息');			
			
			$row['article_content'] = htmlspecialchars_decode($row['article_content']);	
			if($row['article_tags']){
				$ary = explode(',',$row['article_tags']);
				$tags_str = '';
				foreach($ary as $k=>$v){
					$tags_str .=  "'".$v."',";
				}
				$tags_str = trim($tags_str,',');
				$row['article_tags'] = $tags_str;
			}	
	   
	}
	break;
    case "del":
	$result = $main->del_data($id);
	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失败!');
	}

	break;
	case "detail":
	$smarty->mypage($cf_controller, $cf_action, 0);
	$row = $main->get_data_row($id);
	if (!$row)
	my_jump('沒有此信息');
	if($row['goods_photo']){
    $img_ary = explode("|",$row['goods_photo']);
		foreach($img_ary as $v){
			$img_list .= '<a href="'.$v.'" rel="lightbox-group" title="'.strip_tags($text_ary[$k]).'"><img src="'.get_thumb_name($v).'" height="30" /></a>&nbsp;&nbsp;'; 
		}
	}
	$row['img'] = $img_list;
	$row['cate'] = get_cate($row['goods_cate'],$row['goods_sub_cate']);
		if($row['goods_package'])
        $row['package'] = json_decode( stripslashes($row['goods_package']),true);
		if($row['goods_options'])
        $row['options'] = json_decode( stripslashes($row['goods_options']),true);
	
	break;  
	default:
	my_jump('非法操作!');
	break;
}


$smarty->assign(array(
    'id' => $id,
    'row' => $row,
    'data_ary' => $data_ary,	
    'page' => $page,   
    'skey' => $skey,
    'skey2' => $skey2,
    'skey3' => $skey3,
    'skey4' => $skey4,		
	'article_cate' => $article_cate,
));


$smarty->mydisplay();
?>